package org.nashua.tt151.libraries.parsers;

import org.nashua.tt151.libraries.parsers.ProtocolParsing.*;
import org.nashua.tt151.util.MathTools;
import org.nashua.tt151.util.BitwiseTools;


/**
 *
 * @author Brennan
 */
public class Message {
    
    private Command command;
    private Key key;
    private String value;
    private String[] args;
    
    public Message() {
        command = Command.Unknown;
        key = null;
        value = null;
        args = null;
    }
    
    /**
     * Construct a message from a binary string
     * @param bin 
     *
    public Message(String bin) {
        byte header = (byte)bin.charAt(0);
        byte cheader = removebits(header, (byte)2, (byte)7);
        byte kheader = removebits(header, (byte)0, (byte)1);
        kheader = removebits(header, (byte)5, (byte)7);
        
        for (int i = 0; i != Command.ALL.length; i++) {
            Command c = Command.ALL[i];
            
            if (((cheader & c.bitsum) == c.bitsum) && ((cheader ^ c.bitsum) == cheader-c.bitsum)) {
                this.command = c;
                break;
            }
        }
        for (int i = 0; i != Key.ALL.length; i++) {
            Key k = Key.ALL[i];
            if (((kheader & k.bitsum) == k.bitsum) && ((kheader ^ k.bitsum) == kheader-k.bitsum)) {
                this.key = k;
                break;
            }
        }
        
        
    }*/
    
    public String toString() {
        String s;
        
        s = this.command.shorthand + " \n";
        s += this.key.shorthand + " \n";
        s += this.value;
        
        return s;
    }
    
    public Command getCommand() {
        return this.command;
    }
    public Key getKey() {
        return this.key;
    }
    public String getValue() {
        return this.value;
    }
    public String[] getArgs() {
        return this.args;
    }
    
    public void setCommand(Command command) {
        this.command = command;
    }
    public void setKey(Key key) {
        this.key = key;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public void setArgs(String[] args) {
        this.args = args;
    }
    
    
    public String toBinaryString() {
        String bin = "";
        
        byte b = 0;
        b += this.command.bitsum;
        b += this.key.bitsum;
        System.out.println(b);
        
        for (int i = 0; i != 8; i++) {
            short power = (short)MathTools.pow(2.0, i);
            if ((b & power) == power) {
                bin += "1";
            } else {
                bin += "0";
            }
        }
        
        return bin;
    }
    
    
    
    
    
}
